//+------------------------------------------------------------------+
//|                                         WillamsFractalsSound.mq4 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
extern string version="version 2.0";
extern bool turnOnSound=true;

int alertUpBar,alertLowBar,alertUpBrokenBar,alertDownBrokenBar;
double fractalUp, fractalLow, high, low, tempHigh, tempLow;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
    
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
  
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   
   //if(alertBarClosed==true)
   //{
       fractalUp=iFractals(NULL,0,MODE_UPPER,2);
       fractalLow=iFractals(NULL,0,MODE_LOWER,2);
   //}
   //else
   //{
   //    fractalUp=iFractals(NULL,0,MODE_UPPER,0);
  //     fractalLow=iFractals(NULL,0,MODE_LOWER,0);
  // }
  
   //Print("fractalUp" + fractalUp + " fractalLow " +fractalLow);
   
   if (fractalUp!=0 )
   {
      if(alertUpBar!=Bars)
      {
         //Alert("fractal Up");
         if(turnOnSound==true) PlaySound("fractalUp.wav"); 
         alertUpBar=Bars;          
      }
   }

   
   if (fractalLow!=0)
   {
      if(alertLowBar!=Bars)
      {
         //Alert("fractal Low");
         if(turnOnSound==true) PlaySound("fractalDown.wav");   
         alertLowBar=Bars;       
      }
   }

   for(int i=1; i<=1000; i++)
   {
       fractalUp=iFractals(NULL,0,MODE_UPPER,i); 
       
       if(fractalUp!=0)
       {
         high=High[i];
         break;
       }              
   }
   
   for(i=1; i<=1000; i++)
   {
      fractalLow=iFractals(NULL,0,MODE_LOWER,i);
      
       if(fractalLow!=0)
       {
         low=Low[i];
         break;
       }  
   }   

 //PlaySound("fractalDownBroken.wav");   
   if(Bid>high && high!=0  && tempHigh!=high)
   {
     Print("Bid>high " +  Bid + " " + high);
      if(turnOnSound==true) PlaySound("fractalUpBroken.wav");   
      tempHigh=high;
   }
   
   if(Bid<low && low!=0 && tempLow!=low)
   {
      Print("Bid<low " +  Bid + " " + low);
      if(turnOnSound==true) PlaySound("fractalDownBroken.wav");   
      tempLow=low;
   }
             
   
   //Print("high "+ high +" tempHigh" + tempHigh + " low "+ low +" tempLow" + tempLow);
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+